import React, { useState } from 'react'

interface FilterParams {
    page: number;
    size: number;
}

interface ModalParams {
    title: string;
    size: number;
    isShow: boolean
}
//table数据高阶组件封装
export default function TableHoc(WrappedComponent: any) {
    return function TableWrap() {
        let basicParams: FilterParams = {
            page: 1,
            size: 10,
        }
        let [data, setData] = useState([]);
        let [loading, setLoading] = useState<boolean>(false);
        let [params, setParams] = useState<object>({ ...basicParams })
        let [total, setTotal] = useState<number>(0)
        let [rowKeys, setRowKeys] = useState<Array<number>>([])
        //弹出层参数
        let [showModal, setShowModal] = useState<Boolean>(false)
        //重置
        const handleReset = (searchList: (page:number) => void) => {
            let initParams = { ...params }
            for (const k in initParams) {
                initParams[k] = '';
            }
            let newParams = { ...initParams, ...basicParams };
            setParams(newParams);
            setTimeout(()=>{
                searchList(1);
            })
        }
        //添加
        const handleAdd = () => {
            setShowModal(true)
        }
        //切换页码
        const handleChangeFilter = (pagination: { pageSize: number; current: number; }, searchTable: (page: number) => void) => {
            searchTable(pagination.current);
        }
        //多选项
        const handleSelect = (key: React.SetStateAction<number[]>) => {
            setRowKeys(key)
        }

        return <>
            <WrappedComponent
                loading={loading}
                data={data}
                params={params}
                total={total}
                rowKeys={rowKeys}
                showModal={showModal}
                setLoading={setLoading}
                setData={setData}
                setParams={setParams}
                setTotal={setTotal}
                setRowKeys={setRowKeys}
                setShowModal={setShowModal}
                reset={handleReset}
                add={handleAdd}
                handleChangeFilter={handleChangeFilter}
                handleSelect={handleSelect}
            ><span {...params}>1111</span></WrappedComponent>
        </>
    }
}
